//
//  KYSettingCheckGroup.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSettingCheckGroup.h"
#import "KYSettingCheckItem.h"
#import "KYSettingLabelItem.h"

@implementation KYSettingCheckGroup
- (KYSettingCheckItem *)checkedItem
{
    for (KYSettingCheckItem *item in self.items) {
        if (item.isChecked) return item;
    }
    return nil;
}

- (void)setCheckedItem:(KYSettingCheckItem *)checkedItem
{
    for (KYSettingCheckItem *item in self.items) {
        item.checked = (item == checkedItem);
    }
    self.sourceItem.text = checkedItem.title;
}

- (NSInteger)checkedIndex
{
    for (int i = 0; i < self.items.count; i++) {
        KYSettingCheckItem *item = self.items[i];
        if (item.isChecked) return i;
    }
    return -1;
}

- (void)setCheckedIndex:(NSInteger)checkedIndex
{
    if (checkedIndex < 0 || checkedIndex >= self.items.count) return;
    
    self.checkedItem = self.items[checkedIndex];
}

- (void)setItems:(NSArray *)items
{
    [super setItems:items];
    
    self.sourcrItem = self.sourceItem;
}

- (void)setSourcrItem:(KYSettingLabelItem *)sourceItem
{
    _sourceItem = sourceItem;
    
    for (KYSettingCheckItem *item in self.items) {
        item.checked = [item.title isEqualToString:sourceItem.text];
    }
}

@end
